<template>
  <div class="bottom-menu">
    <check-button class="select-all" @checkBtnClick="checkBtnClick" v-model:value="isSelectAll"></check-button>
    <span>全选</span>
    <span class="total-price">合计: ¥{{totalPrice}}</span>
    <div class="div-buy-product">
      <span class="buy-product">去计算({{$store.getters['cart/cartCount']}})</span>
    </div>
  </div>
</template>

<script>
  import CheckButton from './CheckButton'
export default {
  name: 'IshopBottombar',
  components:{
    CheckButton
  },
  data() {
    return {
      
    };
  },
  computed:{
    totalPrice() {
      const cartList = this.$store.getters['cart/cartList']
      return cartList.filter(item =>{ 
        return item.checked 
      }).reduce((preValue, item)=>{
        return preValue + item.count * item.newPrice
      }, 0).toFixed(2)
    },
    isSelectAll: function () {
      let result = this.$store.getters['cart/cartList'].find(item => item.checked === false)
      return  result === undefined;
    }
  },
  methods: {
    checkBtnClick: function () {
      // 1.判断是否有未选中的按钮
      let isSelectAll = this.$store.getters['cart/cartList'].find(item => !item.checked);

      // 2.有未选中的内容, 则全部选中
      if (isSelectAll) {
        this.$store.getters['cart/cartList'].forEach(item => {
          item.checked = true;
        });
      } else {
        this.$store.getters['cart/cartList'].forEach(item => {
          item.checked = false;
        });
      }
    }
  },
};
</script>

<style scoped>
  .bottom-menu {
    position: fixed;
    bottom: 50px;
    left: 0;
    right: 0;
    height: 44px;
    background-color: #eee;
    box-shadow: 0 -2px 3px rgba(0, 0, 0, .2);
    font-size: 14px;
    color: #888;
    box-sizing: border-box;
    display: flex;
    align-items: center;
  }
  .bottom-menu .select-all {
    padding: 6px 10px;
  }

  .bottom-menu .total-price {
    margin-left: 15px;
    font-size: 16px;
    color: #666;
  }
  .bottom-menu .div-buy-product {
    flex: 1;
  }
  .bottom-menu .buy-product {
    background-color: orangered;
    color: #fff;
    width: 100px;
    height: 44px;
    text-align: center;
    line-height: 44px;
    float: right;
  }
</style>